var myHttp = {
  baseUrl:"http://120.26.233.25:9080/api/",
  adminUrl:"http://120.26.233.25:8080/",
  dataUrl:"http://121.40.151.232:8080/",
  apiUrl:"http://120.26.233.25:8088/",
  //baseUrl:"http://admin.diaoqianyaner.com.cn/",
  //apiUrl:"http://api.diaoqianyaner.com.cn/",
  asoUrl:"http://backend.cqaso.com/",
  //baseUrl:"/",
  dataType:"json",
  contentType:"application/json;charset=UTF-8",
  crossDomain:true,
  header:{
    "Content-Type":"application/json;charset=UTF-8",
    //"Request-Token":"dwj"
  },
  isFunc:function(func){
    return (typeof(func) === "function");
  },
  getDate : function(date){
    var year = date.getFullYear();
    var month = date.getMonth()+1;
    var day = date.getDate();
    month = month > 9? month : "0"+month;
    day   = day  >9 ? day:"0"+day;
    return year+"-"+month+"-"+day;
  },
  getQuery : function(){  //获取url中"?"符后的字串
    var url = location.search;
    var theRequest = {};
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) {
        var value = strs[i].split("=");
        theRequest[value[0]]=unescape(value[1]);
      }
    }
    return theRequest;
  },
  getFormJson:function(form) {
    var o = {};
    var a = $(form).serializeArray();
    $.each(a, function (index,data) {
       if(data.name !== undefined){
         o[data.name] = data.value || "";
       }
    });
    return o;
  },
  getUrl:function(url){
    if(url.indexOf("http://") < 0){
      url = this.baseUrl + url;
    }
    return url;
  },
  base:function(ajaxdata,callback,failback){
    // ajaxdata.dataType = 'jsonp';
    // ajaxdata.crossDomain = true;
    ajaxdata.xhrFields = {withCredentials: true};
    var self = this;
    //ajaxdata.dataType    = self.dataType;
    //ajaxdata.contentType = self.contentType;
    //ajaxdata.crossDomain = self.crossDomain;
    $.ajax(ajaxdata).then(function(data,status,xhr){
      window.xhr = xhr;
      if(self.isFunc(callback)){
          callback(data);
      }
    },function(xhr,status,error){
      console.log(xhr);
      if(self.isFunc(failback)){
        var msg = "网络异常";
        if(xhr.responseText){
          var back = JSON.parse(xhr.responseText);
          msg = back.msg;
        }
        failback(msg);
      }
    }).always(function(){
      console.log("finish");
    });
  },
  getJSON:function(url,callback,failback){
    var self = this;
    var ajaxdata = {
      url:self.getUrl(url)
    };
    self.base(ajaxdata,callback,failback);
  },
  post:function(url,data,callback,failback){
    var self = this;
    var ajaxdata = {
      method:"POST",
      data:data,
      url:self.getUrl(url)
    };
    self.base(ajaxdata,callback,failback);
  },
  postJSON:function(url,data,callback,failback){
    var self = this;
    var ajaxdata = {
      headers:self.header,
      method:"POST",
      data:JSON.stringify(data),
      url:self.getUrl(url)
    };
    self.base(ajaxdata,callback,failback);
  },
  putData:function(url,data,callback){
    var self = this;
    var ajaxdata = {
      headers:self.header,
      method:"PUT",
      data:JSON.stringify(data),
      url:self.getUrl(url)
    };
    self.base(ajaxdata,callback);
  },
  uploadImage:function(url,data,callback){
    var self = this;
    var ajaxdata = {
      method:"POST",
      data:data,
      url:self.apiUrl+url,

      //attention!!!
      contentType: false,
      cache: false,
      processData:false,
    };
    self.base(ajaxdata,callback);
  },
  uploadImage2:function(url,data,callback){
    var self = this;
    var ajaxdata = {
      method:"POST",
      data:data,
      url:self.getUrl(url),

      //attention!!!
      contentType: false,
      cache: false,
      processData:false,
    };
    self.base(ajaxdata,callback);
  },
  put:function(url,callback,failback){
    var self = this;
    var ajaxdata = {
      headers:self.header,
      method:"PUT",
      url:self.getUrl(url)
    };
    self.base(ajaxdata,callback,failback);
  },
  patch:function(url,data,callback,failback){
    var self = this;
    var ajaxdata = {
      data:data,
      method:"PATCH",
      url:self.getUrl(url)
    };
    self.base(ajaxdata,callback,failback);
  },
  delete:function(url,callback,failback){
    var self = this;
    var ajaxdata = {
      headers:self.header,
      method:"DELETE",
      url:self.getUrl(url)
    };
    self.base(ajaxdata,callback,failback);
  },
};
